package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.BusinessCooperation;
import com.letv.whatslive.model.Cooperation;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by shenyue on 16-3-2.
 */
public class CooperationConvert {

    public static DBObject castCooperationToDBObject(Cooperation cooperation) {
        DBObject dbo = new BasicDBObject();
        if (cooperation.getId() != null) {
            dbo.put("_id", cooperation.getId());
        }
        if(cooperation.getBusinessCooperationId()!=null){
            dbo.put("businessCooperationId", cooperation.getBusinessCooperationId());
        }
        if(StringUtils.isNotBlank(cooperation.getCooperationName()))
            dbo.put("cooperationName", ObjUtils.toString(cooperation.getCooperationName()));
        if(StringUtils.isNotBlank(cooperation.getCooperationContent()))
            dbo.put("cooperationContent", ObjUtils.toString(cooperation.getCooperationContent()));
        if(StringUtils.isNotBlank(cooperation.getOurContacterDepartment()))
            dbo.put("ourContacterDepartment", ObjUtils.toString(cooperation.getOurContacterDepartment()));
        if(StringUtils.isNotBlank(cooperation.getOurContacter()))
            dbo.put("ourContacter", ObjUtils.toString(cooperation.getOurContacter()));
        dbo.put("createTime", ObjUtils.toLong(cooperation.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", ObjUtils.toLong(cooperation.getUpdateTime(), System.currentTimeMillis()));
        if(cooperation.getDiscussionTime()!=null)
            dbo.put("discussionTime", ObjUtils.toLong(cooperation.getDiscussionTime()));
        if(cooperation.getPerformanceStartTime()!=null)
            dbo.put("performanceStartTime", ObjUtils.toLong(cooperation.getPerformanceStartTime()));
        if(cooperation.getPerformanceEndTime()!=null)
            dbo.put("performanceEndTime", ObjUtils.toLong(cooperation.getPerformanceEndTime()));
        if(StringUtils.isNotBlank(cooperation.getRights()))
            dbo.put("rights", ObjUtils.toString(cooperation.getRights()));
        if(StringUtils.isNotBlank(cooperation.getRemarks()))
            dbo.put("remarks", ObjUtils.toString(cooperation.getRemarks()));
        return dbo;
    }

    public static Cooperation castDBObjectToCooperation(DBObject dbObject){
        if(dbObject != null) {
            Cooperation cooperation = new Cooperation();
            cooperation.setId(ObjUtils.toLong(dbObject.get("_id")));
            cooperation.setCooperationName(ObjUtils.toString(dbObject.get("cooperationName")));
            cooperation.setCooperationContent(ObjUtils.toString(dbObject.get("cooperationContent")));
            cooperation.setOurContacterDepartment(ObjUtils.toString(dbObject.get("ourContacterDepartment")));
            cooperation.setOurContacter(ObjUtils.toString(dbObject.get("ourContacter")));
            cooperation.setOurContacterDepartment(ObjUtils.toString(dbObject.get("ourContacterDepartment")));
            cooperation.setOurContacter(ObjUtils.toString(dbObject.get("ourContacter")));
            cooperation.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            cooperation.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
            cooperation.setDiscussionTime(ObjUtils.toLong(dbObject.get("discussionTime")));
            cooperation.setPerformanceStartTime(ObjUtils.toLong(dbObject.get("performanceStartTime")));
            cooperation.setPerformanceEndTime(ObjUtils.toLong(dbObject.get("performanceEndTime")));
            cooperation.setRights(ObjUtils.toString(dbObject.get("rights")));
            cooperation.setRemarks(ObjUtils.toString(dbObject.get("remarks")));
            return cooperation;
        }else{
            return null;
        }

    }
}
